﻿Namespace Seguranca
    Public Class [String]

        Public Shared Function EncriptaMD5(ByVal _Str As String) As String
            Try
                Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
                Dim rawBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(_Str)
                Dim myHash As Byte() = MD5.ComputeHash(rawBytes)
                Dim Capacity As Integer = (myHash.Length * 2 + (myHash.Length / 8))
                Dim result As New System.Text.StringBuilder(Capacity)
                Dim i As Integer
                For i = 0 To myHash.Length - 1
                    result.Append(BitConverter.ToString(myHash, i, 1))
                Next i
                Return result.ToString().TrimEnd(New Char() {" "c}).ToLower
            Catch ex As Exception
                Throw New Exception("Erro a gerar MD5.")
                Return "0"
            End Try
        End Function

#Region "GerarSenha"

        Public Shared Function GerarSenha(ByVal QtdeCaracteres As Integer) As String
            Return (New Random).Next(CStr(0).PadLeft(QtdeCaracteres, "0"), CStr(0).PadLeft(QtdeCaracteres, "9"))
        End Function

        Public Shared Function GerarSenha() As String
            Return GerarSenha(6)
        End Function

#End Region

        'TODO
        Public Shared Function Encripta(ByVal Str As String) As String
            Return Str
        End Function

    End Class
End Namespace